import Vue from 'vue'
import store from '@/store'
import util from '@/common/util/util.js';
import systemConfig from '@/common/config/systemConfig.js'
import api from '@/api/system/login.js'
import commApi from '@/api/system/common.js'
import {
	http
} from '@/common/util/service.js'
import {
	RouterMount,
	createRouter
} from '@/plugs/hhyang-uni-simple-router/uni-simple-router.js';
//初始化
let myRouters = [...ROUTES]
//#ifdef H5
let myRouter = {
	path: '/',
	meta: {
		ignoreLogin: true
	},
	redirect: {
		path: systemConfig.homePath
	}
}
myRouters.push(myRouter)
//#endif
const router = createRouter({
	//'h5'|'app-plus'|'app-lets'|'mp-weixin'|'mp-baidu'|'mp-alipay'|'mp-toutiao'|'mp-qq'|'mp-360'
	//#ifdef H5
	platform: 'h5',
	//#endif
	//#ifdef MP-360
	platform: 'mp-360',
	//#endif
	//#ifdef APP-PLUS||APP-PLUS-NVUE||APP-NVUE||APP-VUE
	platform: 'app-plus',
	//#endif
	//#ifdef MP-ALIPAY
	platform: 'mp-alipay',
	//#endif
	//#ifdef MP-WEIXIN
	platform: 'mp-weixin',
	//#endif
	//#ifdef MP-BAIDU
	platform: 'mp-baidu',
	//#endif
	//#ifdef MP-TOUTIAO
	platform: 'mp-toutiao',
	//#endif
	//#ifdef MP-QQ
	platform: 'mp-qq',
	//#endif
	routes: myRouters
});
//不控制登录白名单
const whiteList = ['login', 'noLoginError','zlbLogin']
// 登录拦截
router.beforeEach((to, from, next) => {
	let token = store.state.hh_token
	let ignoreLogin = false
	let path = to.path
	console.log(to)
	if (to.meta) {
		let meta = to.meta
		if (meta.ignoreLogin) {
			ignoreLogin = true
		}
	}

	let zlbFlag = false

	// #ifdef H5
	let accessToken = '' //本地登录token
	if (to.name == systemConfig.homeName) {
		if (to.query && to.query.accessToken) {
			accessToken = to.query.accessToken
		} else {
			accessToken = util.getQueryString('accessToken')
		}
		if (accessToken) {
			
		}
	}
	let browerInfo = store.state.hh_browerInfo;
	if (!browerInfo) {
		browerInfo = util.getBrowerInfo()
		let browerInfoParam = {}
		browerInfoParam.name = 'hh_browerInfo'
		browerInfoParam.value = browerInfo
		store.commit('$uStore', browerInfoParam)
	}
	let type = ''
	if (browerInfo) {
		type = browerInfo.browserType
		if (type == 'zlb' || type == 'zlb-alipay'||type == 'alipay') {
			// 浙里办 判断登录
			zlbFlag = true
			if (!store.state.hh_zlb_token) {
				//页面关闭后需要重新登录
				token = ''
			}
		} 
	}
	//#endif
	if (zlbFlag) {
		ignoreLogin = false
	}
	// 判断页面是否登录或者忽略登录
	if (!ignoreLogin && !token) {
		if (whiteList.includes(to.name)) {
			// 在免登录白名单，直接进入
			next()
		} else {
			if (zlbFlag) {

			} else {
				// 未登录 跳转到登录页面
				let queryParams = {}
				let query = to.query
				let i = 0
				for (let key in query) { //遍历json对象的每个key/value对,p为key
					if (i == 0) {
						path = path + "?" + key + "=" + query[key]
					} else {
						path = path + "&" + key + "=" + query[key]
					}
					i++;
				}
				queryParams.gotoPath = path
				queryParams.userType = store.state.hh_userType
				next({
					path: systemConfig.loginPath,
					query: queryParams
				})
			}

		}
	} else {
		next()
	}
})

// 全局路由后置守卫
router.afterEach((to, from) => {
	let serviceCode = to.query.serviceCode
	let serviceName = to.query.serviceName
	let serviceId = to.query.serviceId
	let menuId = to.query.menuId
	let menuTitle = to.query.headerTitle
	let infoName = to.query.infoName
	//#ifdef H5
	if (!serviceCode) {
		serviceCode = util.getQueryString('serviceCode')
	}
	if (!serviceName) {
		serviceName = util.getQueryString('serviceName')
	}
	if (!serviceId) {
		serviceId = util.getQueryString('serviceId')
	}
	if (!menuId) {
		menuId = util.getQueryString('menuId')
	}
	if (!menuTitle) {
		menuTitle = util.getQueryString('headerTitle')
	}
	if (!infoName) {
		infoName = util.getQueryString('infoName')
	}
	//#endif
	if (!menuTitle) {
		if (meta) {
			menuTitle = meta.title
		}
	}
	if (!infoName) {
		infoName = menuTitle
	}
	let linkPath = to.path
	let meta = to.meta
	if (!menuId) {
		if (meta) {
			menuId = meta.menuId
		}
	}
	let params = {}
	params.menuId = menuId
	params.serviceCode = serviceCode
	params.serviceName = serviceName
	params.serviceId = serviceId
	params.menuTitle = menuTitle
	params.infoName = infoName
	params.linkPath = linkPath
	if(linkPath=='/pages/tabBar/home/zlbLogin' || linkPath=='/pages/system/web-view/webViewLogin'){
		return;
	}
	http.post(commApi.saveServiceLog, params).then(res => {
		if (res.data && res.data.code == 0) {
			console.log("服务日志保存成功")
		} else {
			console.log("服务日志保存失败")
		}

	}).catch(error => {})
})

export {
	RouterMount,
	router
}
